<!-- 会员列表> 附件管理 -->
<template>
  <div>
    <div class="table-operator">
      <UploadFile @uploadSuccess="uploadSuccess" />
    </div>
    <s-table size="default" ref="table" rowKey="userId" :rowSelection="null" :columns="columns" :data="loadData">
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="delById(record.id)">删除</a>
        </template>
        <template>
          <a-divider type="vertical" />
          <a @click="handleDownLoad(record)">下载</a>
        </template>
      </span>
    </s-table>
  </div>
</template>

<script>
import { STable } from '@/components'
import UploadFile from '@/components/UploadFile/index.vue'
import { accessoryPageList, accessoryAdd, accessoryDel } from '@/api/member/member'
export default {
  components: {
    STable,
    UploadFile
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: '附件名称',
          dataIndex: 'name'
        },
        {
          title: '附件大小',
          dataIndex: 'sizeStr'
        },
        {
          title: '上传人',
          dataIndex: 'uploadUserName'
        },
        {
          title: '上传时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          width: '150px',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize,
          memberId: this.memberId
        }
        return accessoryPageList(Object.assign(params))
      },
      memberId: undefined
    }
  },
  created() {
    this.memberId = this.$route.query.id
  },
  methods: {
    handleReload() {
      this.$refs.table.refresh()
    },
    async uploadSuccess(url, { size, name }) {
      await accessoryAdd({
        memberId: this.memberId,
        size,
        url,
        name
      })
      this.handleReload()
    },
    delById(id) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该附件?',
        onOk() {
          accessoryDel(id).then(() => {
            _this.$message.success(`删除成功`)
            _this.handleReload()
          })
        }
      })
    },
    handleDownLoad({ url, name }) {
      window.open(url + `?attname=${name}`, '_self')
    }
  }
}
</script>
<style scoped lang="less"></style>
